package com.huang825172.sbog.Tabs.Mine.InfoEditPage.View;

import android.app.Activity;
import android.content.Intent;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huang825172.sbog.BaseFlux.Dispatcher;
import com.huang825172.sbog.BaseFlux.Store.IDataChangedListener;
import com.huang825172.sbog.R;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.Action.ActionCreator;
import com.huang825172.sbog.Tabs.Mine.InfoEditPage.Store.Store;
import com.huang825172.sbog.Util.TAGs;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;
import me.rosuh.filepicker.config.FilePickerManager;

public class InfoEditPage extends AppCompatActivity implements View.OnClickListener, IDataChangedListener {

    @BindView(R.id.InfoEditPageIDEditText)
    EditText idEditText;
    @BindView(R.id.InfoEditPageEmailEditText)
    EditText emailEditText;
    @BindView(R.id.InfoEditPageGenderEditText)
    EditText genderEditText;
    @BindView(R.id.InfoEditPageLocationEditText)
    EditText locationEditText;
    @BindView(R.id.InfoEditPageCommentEditText)
    EditText commentEditText;
    @BindView(R.id.InfoEditPageAgeEditText)
    EditText ageEditText;

    @BindView(R.id.InfoEditPagePhotoImageView)
    CircleImageView photoImageView;

    @BindView(R.id.InfoEditPageSubmitButton)
    TextView submitButton;

    @BindView(R.id.InfoEditPageRootView)
    ConstraintLayout rootView;

    private Store store;
    private ActionCreator actionCreator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info_edit_page);

        ButterKnife.bind(this);

        store = new Store(this);
        store.setDataChangedListener(this);
        Dispatcher.getSingleton().register(store);
        actionCreator = new ActionCreator();

        photoImageView.setOnClickListener(this);
        submitButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.InfoEditPageSubmitButton: {
                String username = idEditText.getEditableText().toString();
                String email = emailEditText.getEditableText().toString();
                String gender = genderEditText.getEditableText().toString();
                String location = locationEditText.getEditableText().toString();
                String comment = commentEditText.getEditableText().toString();
                String age = ageEditText.getEditableText().toString();
                actionCreator.createSubmitAction(username, email, gender, location, comment, age);
                break;
            }
            case R.id.InfoEditPagePhotoImageView: {
                FilePickerManager.INSTANCE
                        .from(this)
                        .forResult(FilePickerManager.REQUEST_CODE);
                break;
            }
            default:
                break;
        }
    }

    @Override
    public void onDataChanged(String label) {
        switch (label) {
            case "InfoEditPageEditSuccess": {
                Snackbar.make(rootView, "编辑成功", Snackbar.LENGTH_SHORT).show();
                break;
            }
            case "InfoEditPagePhotoSelectAction": {
                File file = new File(store.getPhotoFilePath());
                Log.d(TAGs.INFO, store.getPhotoFilePath());
                Glide.with(this)
                        .load(file)
                        .placeholder(R.mipmap.ic_launcher)
                        .into(photoImageView);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case FilePickerManager.REQUEST_CODE: {
                if (resultCode == Activity.RESULT_OK) {
                    actionCreator.createPhotoSelectAction(FilePickerManager.INSTANCE.obtainData().get(0));
                }
                break;
            }
            default:
                break;
        }
    }
}
